﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Base
{
    public partial class frmaddyh : Form
    {
        public frmaddyh()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void frmaddyh_Load(object sender, EventArgs e)
        {
            selectzhiyuan();
        }
        /// <summary>
        /// 查找所有员工
        /// </summary>
        public void selectzhiyuan()
        {
            string sql = "SELECT [ecode] as 工号 ,[ename] as 姓名 ,[ephone] as 联系电话 ,[einput] as 入职日期,[eoutput]as 离职日期 ,[eremark] as 备注 ,[eaddress] as 联系地址 ,[ecard] as 身份证号码,[emobile] as 手机 FROM [employee]";
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //把选中的职员插入到userlogin表中
            //查询职员表中的工号，密码
            try
            {
                string sql = string.Format("SELECT employee.ename,[ecode] ,[epassword] FROM [employee] where ecode='{0}' ", this.dataGridView1.SelectedCells[0].Value.ToString());
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                string password = "";
                while (sdr.Read())
                {
                    password = sdr["epassword"].ToString();
                }
                //将密码工号姓名插入到userlogin表
                string sql1 = string.Format("INSERT INTO [LoginUser] ([LoginUserID] ,[LoginPassword] ,[username] ,[xiaofei] ,[huiyuanguanli] ,[spsz] ,[ygsz] ,[jcxf] ,[xtsz] ,[yhsz] ,[sjsj] ,[hycztj] ,[yggztj] ,[myszhz] ,[jcxfmx] ,[hycz],[hylx] ,[zmqd] ,[sjbf] ,[sjhf] ,[szkmgl] ,[sprk] ,[zjkk],[xsmx] ,[jhmx] ,[kcbb] ,[kcpd],收入明细,kaiban,spfl,jcxmgl) VALUES ('{0}' ,'{1}' ,'{2}' ,0 ,0 ,0 ,0 ,0,0 ,0 ,0,0 ,0 ,0,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,1,0,0,0)", this.dataGridView1.SelectedCells[0].Value.ToString(), password, this.dataGridView1.SelectedCells[1].Value.ToString());
                SqlConnection conn1 = new SqlConnection(constr);
                conn1.Open();
                SqlCommand comm1 = new SqlCommand(sql1, conn1);
                comm1.ExecuteNonQuery();
                conn.Close();
                conn1.Close();
                this.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show("不能重复选择");
            }
        }
    }
}
